package com.puntodonna.fogliocassa.client.widget;

import java.text.ParseException;
import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DoubleBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;

public class Movimento extends Composite {

	private static MovimentoUiBinder uiBinder = GWT.create(MovimentoUiBinder.class);

	interface MovimentoUiBinder extends UiBinder<Widget, Movimento> {
	}

	@UiField
	protected DateBox dataBox;
	@UiField
	protected DoubleBox importoTextBox;
	@UiField
	protected DoubleBox ivaTextBox;
	@UiField
	protected ListBox tipoMovimentoListBox;
	@UiField
	protected TextArea causaleTextArea;
	@UiField
	protected Label messageLbl;
	@UiField
	protected Button okBtn;
	@UiField
	protected Button clearBtn;
	@UiField
	protected Button cancelBtn;

	public Movimento() {
		initWidget(uiBinder.createAndBindUi(this));
		NumbersOnly keyPressHandler = new NumbersOnly();
		getImportoTextBox().addKeyPressHandler(keyPressHandler);
		getIvaTextBox().addKeyPressHandler(keyPressHandler);
		getIvaTextBox().setText("21");
		getTipoMovimentoListBox().addItem("Credito");
		getTipoMovimentoListBox().addItem("Debito");
		getDataBox().setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat("dd/MM/yyyy")));
		getDataBox().setValue(new Date());
	}

	public void reset() {
		getImportoTextBox().setText(null);
		getIvaTextBox().setText("21");
		getTipoMovimentoListBox().setItemSelected(0, true);
		getCausaleTextArea().setText(null);
		setMessage(null);
		getDataBox().setValue(new Date());
	}

	class NumbersOnly implements KeyPressHandler {

		@Override
		public void onKeyPress(KeyPressEvent event) {
			DoubleBox doubleBox = (DoubleBox) event.getSource();
			if (!Character.isDigit(event.getCharCode()) && event.getCharCode() != '.') {
				doubleBox.cancelKey();
			}

			try {
				doubleBox.getValueOrThrow();
			} catch (ParseException e) {
				Window.alert(doubleBox.getText() + " is not a valid number!");
				doubleBox.cancelKey();
			}
		}
	}

	public DoubleBox getImportoTextBox() {
		return importoTextBox;
	}

	public DoubleBox getIvaTextBox() {
		return ivaTextBox;
	}

	public ListBox getTipoMovimentoListBox() {
		return tipoMovimentoListBox;
	}

	public TextArea getCausaleTextArea() {
		return causaleTextArea;
	}
	
	public Button getCancelBtn() {
		return cancelBtn;
	}
	
	public Button getClearBtn() {
		return clearBtn;
	}
	
	public Button getOkBtn() {
		return okBtn;
	}
	
	public void setMessage(String message){
		this.messageLbl.setText(message);
	}
	
	public DateBox getDataBox() {
		return dataBox;
	}

}
